<template>
  <van-popup v-model="show" position="bottom" round safe-area-inset-bottom @open="openHandle" @closed="closedHandle">
    <div class="attribute">
      <p>选择规格</p>
      <ul class="list">
        <li v-for="(l,i) in comlist" :key="i" class="list-item">
          <p>{{ l.key }}</p>
          <ul class="item">
            <li v-for="(item,n) in l.specs || []" :key="n" :class="{active: actived(item,i)}" @click.stop="clickHandle(item, i)">{{ item.value }}</li>
          </ul>
        </li>
      </ul>
    </div>
    <div class="btn">
      <van-button class="cus-default" round @click="cancelHandle">取消</van-button>
      <van-button class="cus-primary" round type="primary" @click="confirmHandle">确定</van-button>
    </div>
  </van-popup>
</template>
<script>
export default {
  name: 'Attribute',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    list: {
      type: Array,
      default: () => []
    },
    skus: {
      type: Array,
      default: () => []
    },
    skuId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      show: false,
      selected: {},
      // eslint-disable-next-line vue/no-reserved-keys
      rawSelected: {}
    }
  },
  computed: {
    comlist() {
      return this.list.map(e => {
        if (e.specs) {
          e.specs = e.specs.map(s => {
            if (!s.productAttributeId || !s.productAttrItemId) {
              s.productAttributeId = e.key
              s.productAttrItemId = s.value
            }
            return s
          })
        }
        return e
      })
    }
  },
  watch: {
    value: {
      handler(v) {
        this.show = v
      },
      immediate: true
    },
    skuId() {
      this.setSelected(this.comlist)
    },
    comlist(l) {
      this.setSelected(l)
    }
  },
  methods: {
    // 设置选中项
    setSelected(list) {
      if (!this.skuId) return
      const selected = {}
      let sku
      for (const item of this.skus) {
        if (item.id === this.skuId) {
          sku = item
          break
        }
      }
      if (sku === undefined) return
      const skuSpecs = JSON.parse(sku.skuSpecs || '[]')
      for (let i = 0; i < list.length; i++) {
        const v = list[i]
        for (const s of v.specs || []) {
          for (const item of skuSpecs) {
            if (item.key === s.productAttributeId && item.value === s.productAttrItemId) {
              this.$set(selected, i, s)
              break
            }
          }
        }
      }
      if (Object.keys(selected).length > 0) {
        this.selected = selected
        this.rawSelected = {}
        this.$emit('selected', this.$deepCopy(this.selected))
      }
    },
    actived(item, i) {
      const v = this.selected[i]
      if (v && v.productAttributeId === item.productAttributeId && v.productAttrItemId === item.productAttrItemId) {
        return true
      }
      return false
    },
    openHandle() {
      this.rawSelected = this.$deepCopy(this.selected)
    },
    closedHandle() {
      this.rawSelected = {}
      this.$emit('input', false)
    },
    cancelHandle() {
      this.selected = this.rawSelected
      this.closedHandle()
    },
    confirmHandle() {
      if (Object.keys(this.selected).length < this.comlist.length) {
        this.$toast('请选择完整规格属性')
        return
      }
      const selected = Object.entries(this.selected)
      let selectedSku
      if (selected.length > 0) {
        for (const sku of this.skus) {
          const skuSpecs = JSON.parse(sku.skuSpecs || '[]')
          let flag = 0
          for (const ss of skuSpecs) {
            // eslint-disable-next-line no-unused-vars
            for (const [i, s] of selected) {
              if (ss.key === s.productAttributeId && ss.value === s.productAttrItemId) {
                flag++
                break
              }
            }
          }
          if (flag > 0 && flag === skuSpecs.length) {
            selectedSku = sku
            break
          }
        }
      }
      if (selectedSku === undefined) {
        this.$toast('无所选规格商品')
        return
      }
      this.$emit('refresh', selectedSku.id)
      this.$emit('selected', this.$deepCopy(this.selected))
      this.closedHandle()
    },
    clickHandle(item, i) {
      if (this.selected[i] === item) {
        this.$delete(this.selected, i)
        return
      }
      this.$set(this.selected, i, item)
      // this.selected[i] = item
    }
  }
}
</script>
<style lang="less" scoped>
.attribute{
  min-height: 60vh;
  padding: 0 12px 12px 12px;
  box-sizing: border-box;
  &>p{
    color: #999;
    font-size: 16px;
    text-align: center;
    height: 40px;
    line-height: 40px;
  }
  .list{
    &>.list-item{
      &>p{
        color: #333;
        font-size: 14px;
        padding: 10px 0;
      }
    }
    .item{
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      &>li{
        flex: 0 0 auto;
        font-size: 14px;
        color: #333;
        padding: 5px 12px;
        border-radius: 2px;
        // border: 1px solid black;
        background-color: #F5F6F7;
        margin-right: 10px;
        margin-bottom: 10px;
        box-sizing: border-box;
      }
      .active{
        background-color: #F1FBF8;
        outline: 1px solid #16BC83;
        color: #16BC83;
      }
    }
    .list-item+.list-item{
      border-top: 1px solid #BDBDBD;
      // margin-top: 10px;
    }
  }
}
  .btn{
    display: flex;
    justify-content: space-between;
    padding: 12px;
    &>*{
      flex: 1 1 50%;
    }
    &>button:not(:last-child){
      margin-right: 10px;
    }
  }
</style>
